<?php

/*
 * This file is part of the Dektrium project.
 *
 * (c) Dektrium project <http://github.com/dektrium/>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace app\models;

use Yii;
use yii\base\Model;

/**
 * Registration form collects user input on registration process, validates it and creates new User model.
 *
 * @author Dmitry Erofeev <dmeroff@gmail.com>
 */
class RegistrationForm extends Model
{
    public $email;
    /**
     * @var string Username
     */
    public $username;

    /**
     * @var string Password
     */
    public $password;

    public $confirmPassword;

    public $mobile;

    public $code;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['username', 'password', 'confirmPassword', 'mobile', 'code'], 'required'],
            ['mobile', 'validateMobile'],
            ['confirmPassword', 'validateEqual'],
        ];
    }

    public function validateMobile($attribute)
    {
        if (!preg_match('/^1[0-9]{10}$/', $this->$attribute)) {
            $this->addError($attribute, '手机号格式不正确');
        }
    }

    /**
     * Validates the password.
     * This method serves as the inline validation for password.
     *
     * @param string $attribute the attribute currently being validated
     * @param array $params the additional name-value pairs given in the rule
     */
    public function validateEqual($attribute, $params)
    {
        if (!$this->hasErrors()) {
            if ($this->password != $this->confirmPassword) {
                $this->addError($attribute, '两次输入的密码不一致');
            }
        }
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'username' => '用户名',
            'mobile' => '手机号',
            'code' => '验证码',
            'password' => '密码',
            'confirmPassword' => '确认密码',
        ];
    }

    /**
     * @inheritdoc
     */
    public function formName()
    {
        return 'register-form';
    }

    /**
     * Registers a new user account. If registration was successful it will set flash message.
     *
     * @return bool
     */
    public function register()
    {
        if (!$this->validate()) {
            return false;
        }
        $registerResult = \Yii::$app->util->postJson('/register', [
            'userName' => $this->username,
            'passWord' => $this->password,
            'mobile' => $this->mobile,
            'code' => $this->code
        ]);
        if ($registerResult['returnCode'] == '200') {
            return true;
        } else {
            $this->addError('code', $registerResult['message']);
        }
    }

    /**
     * Loads attributes to the user model. You should override this method if you are going to add new fields to the
     * registration form. You can read more in special guide.
     *
     * By default this method set all attributes of this model to the attributes of User model, so you should properly
     * configure safe attributes of your User model.
     *
     * @param User $user
     */
    protected function loadAttributes(User $user)
    {
        $user->setAttributes($this->attributes);
    }
}